import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter_qr_reader/flutter_qr_reader.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:imember/Util/ToastUtil.dart';
import 'package:imember/Widget/MyButton.dart';
import 'package:imember/Widget/MyDialog.dart';
import 'package:permission_handler/permission_handler.dart';

class CameraScan extends StatefulWidget {
  Function(String result) onScan;

  CameraScan(this.onScan);

  @override
  State<StatefulWidget> createState() {
    return _CameraScan();
  }
}

class _CameraScan extends State<CameraScan> {
  late QrReaderViewController _controller;
  bool isOk = false;
  String lastContent = "";

  @override
  void initState() {
    questPer();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.stopCamera();
  }

  void onScan(String v, List<Offset> offsets) {
    if (lastContent != v) {
      lastContent = v;
      widget.onScan(v);
      PageUtil().back(context);
    }
  }

  void questPer() async {
    if (await Permission.camera.request().isGranted) {
      setState(() {
        isOk = true;
      });
    }
    if (Platform.isIOS) {
      setState(() {
        isOk = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        width: 469,
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Container(
              height: 52,
              width: double.infinity,
              alignment: Alignment.center,
              decoration: BoxDecoration(color: MyColor.Orange, borderRadius: BorderRadius.vertical(top: Radius.circular(8))),
              child: Text("相機", style: TextStyle(fontSize: 20, color: MyColor.White, fontWeight: FontWeight.bold))),
          Container(
              color: MyColor.White,
              height: 194,
              alignment: Alignment.center,
              width: double.infinity,
              child: Container(
                height: 165,
                width: 300,
                color: MyColor.C4,
                alignment: Alignment.center,
                child: isOk
                    ? RotatedBox(
                        quarterTurns: 3,
                        child: QrReaderView(
                            height: 300,
                            width: 165,
                            callback: (container) {
                              this._controller = container;
                              _controller.startCamera(onScan);
                            }))
                    : Container(),
              )),
          Container(
              height: 52,
              width: double.infinity,
              alignment: Alignment.center,
              decoration: BoxDecoration(color: MyColor.White, borderRadius: BorderRadius.vertical(bottom: Radius.circular(8))),
              child:
                  Button("取消", width: 170, height: 30, fontSize: 17, fontColor: MyColor.White, radius: 20, bgColor: MyColor.C99, onTap: () {
                PageUtil().back(context);
              })),
        ]));
  }
}
